typedef unsigned char UCHAR;

struct DLL_STRPICENV { int work[16384]; };

int info_ICO(struct DLL_STRPICENV *env, int *info, int size, UCHAR *fp);
int decode0_ICO(struct DLL_STRPICENV *env, int size, UCHAR *fp, int b_type, UCHAR *buf, int jump);
int decode0_ICOsub(struct DLL_STRPICENV *env, int xsz, int ysz, int x0, int y0, int size, UCHAR *fp, int b_type, UCHAR *buf, int jump);

typedef struct {
	int pnt;
	int tousize;
	int px;
	int py;
	int pbit;
}ICOinfo;

void init_ico(UCHAR *fp, ICOinfo *tuxin)
{
	int c;

	//  huoqutouweizhi
	tuxin->pnt = fp[18] | fp[19] << 8 | fp[20] << 16 | fp[21] << 24;

	// ICOmulu(16)
	tuxin->tousize = fp[tuxin->pnt] | fp[tuxin->pnt+1] << 8 | fp[tuxin->pnt+2] << 16 | fp[tuxin->pnt+3] << 24;	// ??????
	tuxin->px = fp[tuxin->pnt+4] | fp[tuxin->pnt+5] << 8 | fp[tuxin->pnt+6] << 16 | fp[tuxin->pnt+7] << 24;		// ?????fu
	tuxin->py = fp[tuxin->pnt+8] | fp[tuxin->pnt+9] << 8;		// ?????gao?
	c = fp[tuxin->pnt+10] | fp[tuxin->pnt+11] << 8;
	tuxin->py = (c << 16)+tuxin->py / 2;

	// huoquyanseshumu
	tuxin->pbit = fp[tuxin->pnt+14] | fp[tuxin->pnt+15] << 8;	// seshu(???danwei)

	return;
}


int info_ICO(struct DLL_STRPICENV *env, int *info, int size, UCHAR *fp)
{
	ICOinfo *tuxin = (ICOinfo *) (((int *) env) + 128);
	int i, j, k;

	// huoquICOtou
	i = fp[0] | fp[1] << 8;	// Reserved
	j = fp[2] | fp[3] << 8;	// Resource type
	k = fp[4] | fp[5] << 8;	// Icon's number.

	if ((i) || ((j != 1) && (j != 2)) || (k < 1))
		return 0;	// Not icon wenjian.

	init_ico(fp, tuxin);

	info[0] = 0x0004;
	info[1] = 0x0000;
	info[2] = tuxin->px;
	info[3] = tuxin->py;

	return 1;
}

int decode0_ICO(struct DLL_STRPICENV *env, int size, UCHAR *fp, int b_type, UCHAR *buf, int jump)
{
	ICOinfo *tuxin = (ICOinfo *) (((int *) env) + 128);
	int i, e, p, pp, x, y, px2, px3, off, biaoji = 0, mask[2], dmask = 0, col, cnt, *xian;
	int pal[256];

	if (b_type != 4)
		return 2;	/* xiuzhengcuowucanshu */

	init_ico(fp, tuxin);

	// huoquyasuxingshi
	i = fp[tuxin->pnt+16] | fp[tuxin->pnt+17] << 8 | fp[tuxin->pnt+18] << 16 | fp[tuxin->pnt+19] << 24;	// yasuxingshi
	if (i)
		return 1; // Not ready compressed wenjian.

	//  huoqudiaosebanzhunbeihuoquyanma
	p = tuxin->pnt + tuxin->tousize;
	if (tuxin->pbit <= 8) 
	{
		i = 1<<tuxin->pbit;
		if (i > 256)
			return 1; // too many yanses.
		for (cnt = 0; cnt < i; cnt++) 
		{
			pal[cnt] = fp[p] | fp[p+1] << 8 | fp[p+2] << 16;
			p += 4;
		}
	}

	// yanmahuoquzhunbei
	px2 = (tuxin->px * tuxin->pbit + 31) / 32 * 4;
	px3 = (tuxin->px + 31) / 32 * 4;
	off = p;
	pp = px2 * tuxin->py + p;

	// ICOmiaohui
	x = tuxin->px;
	y = tuxin->py;
	mask[0] = 0;
	mask[1] = 0;
	dmask = 0;

	for (cnt = 0; cnt < tuxin->px*tuxin->py; cnt++) 
	{
		if (p >= size)
			break;
		if ((cnt % tuxin->px) == 0) 
		{
			x -= tuxin->px;
			y--;
			p = cnt / tuxin->px * px2 + off;
			mask[0] = 0;
			mask[1] = cnt / tuxin->px * px3 + pp;
			biaoji = -1;
		}
		if (mask[0] == 0) 
		{
			mask[0] = 0x80;
			dmask = fp[mask[1]];
			mask[1]++;
		}
		if (tuxin->pbit > 8) 
		{
			e = 0;
			pal[0] = fp[p] | fp[p+1] << 8 | fp[p+2] << 16;
			p += 3;
	// yinweibubeipinfanshiyong?zhecizhushi
	//		if (pbit > 24) {
	//			h = fp[p];
	//			p++;
	//			p[y * px + x] = col;
	//			phuoqu x,y
	//			r = r * h + (255 -h * rval) /255;
	//			g = g * h + (255 -h * gval) /255;
	//			b = b * h + (255 -h * gval) /255;
	//		}
		} 
		else 
		{
			if (biaoji < 0) 
			{
				i = fp[p];
				p++;
				biaoji = 8-tuxin->pbit;
			}
			e = (i >> biaoji) % (1 << tuxin->pbit);
			if (1<<tuxin->pbit <= e)
				break;
			biaoji -= tuxin->pbit;
		}
		col = 0xC6C6C6;
		if ((dmask & mask[0]) == 0)
			col = pal[e];
		xian = (int*)&buf[y * (tuxin->px * (b_type % 7) + jump)];
		xian[x] = col;
		mask[0] >>= 1;
		x++;
	}
	return 0;
}

int decode0_ICObufen(struct DLL_STRPICENV *env, int xsz, int ysz, int x0, int y0, int size, UCHAR *fp, int b_type, UCHAR *buf, int jump)
{
	return 1;		/*  xiuzhengcuowu */
}
